package com.glory.wxelectricitypda.activity;

import android.os.Handler;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.LinearLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.wxelectricitypda.R;
import com.glory.wxelectricitypda.adapter.EqpUnlockAdapter;
import com.glory.wxelectricitypda.adapter.LotListBeanAdapter;
import com.glory.wxelectricitypda.base.BaseDialogActivity;
import com.glory.wxelectricitypda.bean.EqpLockInfoEntity;
import com.glory.wxelectricitypda.bean.LotBean;
import com.glory.wxelectricitypda.bean.ResponseBean;
import com.glory.wxelectricitypda.utils.NetUtils;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;

public class EqpUnlockActivity extends BaseDialogActivity {

    @BindView(R.id.etEqpId)
    FreeUI_ClearEditText etEqpId;
    @BindView(R.id.etRemark)
    FreeUI_ClearEditText etRemark;
    @BindView(R.id.rvLockInfoList)
    RecyclerView rvLockInfoList;
    @BindView(R.id.llMain)
    LinearLayout llMain;

    private EqpUnlockAdapter mEqpUnlockAdapter;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_eqp_unlock;
    }

    @Override
    protected void initView() {
        rvLockInfoList.setLayoutManager(new LinearLayoutManager(EqpUnlockActivity.this));
        rvLockInfoList.setItemAnimator(new DefaultItemAnimator());
        rvLockInfoList.addItemDecoration(new DividerItemDecoration(EqpUnlockActivity.this, 1));
        mEqpUnlockAdapter = new EqpUnlockAdapter(null);
        mEqpUnlockAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        mEqpUnlockAdapter.isFirstOnly(true);
        rvLockInfoList.setAdapter(mEqpUnlockAdapter);

        freeUI_titleView.setRightListener("解锁", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                JSONObject jsonObject = new JSONObject();
                try {
                    jsonObject.put("EqpID", etEqpId.getText().toString());
                    jsonObject.put("Remark", etRemark.getText().toString());
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                NetUtils.requestPostUtils("releaseEqp","ReleaseEqp",jsonObject,this, new FreeUI_DialogEntityCallBack<ResponseBean<Object>>(
                        new TypeToken<ResponseBean<Object>>() {
                        }.getType(), getSupportFragmentManager(), this) {
                    @Override
                    public void onSuccess(Response<ResponseBean<Object>> response) {
                        if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                            showToast("解锁成功",true);
                            new Handler().postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    finish();
                                }
                            },2000);
                        }
                    }

                    @Override
                    public void onError(Response<ResponseBean<Object>> response) {
                        super.onError(response);
                        loadError(response.getException(), "ReleaseEqp");
                    }
                });
            }
        });
    }

    @Override
    protected void initData() {

        initEdit(etEqpId,s ->{
            JSONObject jsonObject = new JSONObject();
            try {
                jsonObject.put("EqpID", etEqpId.getText().toString());
            } catch (JSONException e) {
                e.printStackTrace();
            }
            NetUtils.requestPostUtils("getEqpLockInfoList","GetEqpLockInfoList",jsonObject,this, new FreeUI_DialogEntityCallBack<ResponseBean<EqpLockInfoEntity>>(
                    new TypeToken<ResponseBean<EqpLockInfoEntity>>() {
                    }.getType(), getSupportFragmentManager(), this) {
                @Override
                public void onSuccess(Response<ResponseBean<EqpLockInfoEntity>> response) {
                    if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                        mEqpUnlockAdapter.setNewData(response.body().getBody().getLockInfoList());
                    }
                }

                @Override
                public void onError(Response<ResponseBean<EqpLockInfoEntity>> response) {
                    super.onError(response);
                    loadError(response.getException(), "GetEqpLockInfoList");
                }
            });
        });

    }

    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        freeUI_titleView.setTitle("设备解锁");
        return new FreeUI_AddViewEntity("header", llMain);
    }
}

